package com.zdwl.controller;

import com.zdwl.pojo.SysUser;
import com.zdwl.service.SysUserService;
import com.zdwl.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2017/12/19.
 */
//想返回视图必须用 controller 而并非 RestController
@RestController
public class UserController{

    @Autowired
    private SysUserService userService;


    /**
     * 登录页面
     * @return
     */
    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public JsonResult login(HttpSession session, String username, String password, Integer area){

       JsonResult jsonResult = new JsonResult();
        return jsonResult;
    }

    @RequestMapping("getAllUser")
    public JsonResult getAll(Integer page, Integer limit, SysUser user){
        JsonResult jsonResult = new JsonResult();
        Map map= new HashMap();
        if(page ==null || limit ==null){
            return  jsonResult;
        }
        try {
             map =  userService.findAll(page,limit,user);
            jsonResult.setData(map.get("data"));
            Long loing = (Long)map.get("count");
            jsonResult.setCount(loing);
            jsonResult.setLimit(limit);
            jsonResult.setPage(page);
        }catch (Exception e){
            e.printStackTrace();
        }
        return jsonResult;
    }

    @PostMapping("addUser")
    public JsonResult getAll(@RequestBody SysUser user){
        JsonResult jsonResult = new JsonResult();
        try {
            userService.saveUser(user);
        }catch (Exception e){
            jsonResult.setCode("500");
            e.printStackTrace();
        }
        return jsonResult;
    }

    @RequestMapping("getUserById")
    public JsonResult getAll(String id){
        JsonResult jsonResult = new JsonResult();
        try {
            List<SysUser> userByIds = userService.getUserByIds(id);
            if(userByIds!=null && userByIds.size()>0){
                jsonResult.setData(userByIds.get(0));
            }
        }catch (Exception e){
            jsonResult.setCode("500");
            e.printStackTrace();
        }
        return jsonResult;
    }
    @PostMapping("deleteByUsers")
    public JsonResult deleteUser(@RequestBody List<SysUser> list){
        JsonResult jsonResult = new JsonResult();
        try{
            userService.delUserByUsers(list);
        }catch (Exception e){
            jsonResult.setCode("500");
            e.printStackTrace();
        }
        return jsonResult;
    }
}
